<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>医药库</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="/system_assets/css/bootstrap.min.css" rel="stylesheet">
	<link href="/system_assets/css/style.css" rel="stylesheet">
	<link href="/system_assets/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" href="/plugins/SweetAlert/sweetalert.css" />
</head>
<body>
	<div class="container-fluid">
		<div class="om-ctl-mes">
			<div class="title">
				<div class="col-xs-12"><span class="fa fa-list-ul"> 系统设置</span><b class="fa fa-caret-right"></b><i>医药库</i></div>
			</div>
			<div class="seltemple">
				<div class="col-xs-12">
					<form class="form-inline"  method="get" action="/admin_medicine">
                    <div class="col-xs-3 yyk-data">
                    
                    <label for="exampleInputName2">添加时间</label>
                    <div class="input-group date form_date" data-date="" data-date-format="yyyy-mm-dd" data-link-field="updated">
                    <input class="form-control" id="form-control" name="medicine_date" size="16" id="jcnow" readonly="" placeholder="选择添加日期" type="text" onclick="WdatePicker();" value="<?=$pMedicinedate?>">
                    </div>
                    <input id="updated" value="" name="updated" type="hidden"><br>
                    		
                    </div>			
					<div class="col-xs-3 mes-search"><label>关键词搜索</label><input type="text" placeholder="药品名" name="key_name" value="<?=$pKeyName?>"><button id="search" type="button">搜索</button></div>
					<div class="col-xs-3 mes-btn yyk-btn"><p id="off-btn">新增药品</p></div>
				</form>
				</div>
			</div>
			<div  class="mes-conttent col-xs-12">
				<table class="table yyk-table">
	              <thead>
	             	<tr>
			        <th>编号</th>
			        <th>药品图片</th>
			        <th>药品名</th>
			        <th>添加日期</th>
			        <th>添加人员</th>
			        <th>备注</th>
			        <th>操作</th>
		            </tr>
	              </thead>
	             <tbody>
	             <?foreach($tDatas as $tRow){?>
		           <tr>
		           	 <td><?=$tRow['id']?></td>
			         <td><img src="<?=$tRow['img']?>"></td>
                     <td><span class="say"><?=$tRow['name']?></span><small class="hiddendiv"></small></td>
			         <td><?=date('Y-m-d H:i',$tRow['created'])?></td>
			         <td><?=$tRow['username']?></td>
			         <td><span><?=$tRow['remarks']?></span><small class="hiddendiv"></small></td>
			         <td>
                     <small class="edit" data="<?=$tRow['id']?>">编辑</small>
                     <button type="button" onclick="cg_confirm_del('/admin_medicine/delete?id=<?=$tRow['id']?>','/admin_medicine');">删除</button>
			         </td>
		           </tr>
		           <?}?>
	            </tbody>
              </table>
              <?=$tShow?>
			</div>
		</div>
    </div>
     <script type="text/javascript" src="/system_assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="/system_assets/js/jq.js"></script>
    <script type="text/javascript" src="/plugins/datepicker/WdatePicker.js" charset="UTF-8"></script>
<script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="/plugins/SweetAlert/sweetalert.min.js"></script><!--alert插件-->
<script src="/js/jquery.form.js"></script><!--form表单插件-->
    <script>
$(function(){
//提交
    var options={
        beforeSubmit : showRequest,    // 提交前的回调函数
        success : showResponse,    // 提交后的回调函数
        dataType : "json",    // html（默认）、xml、script、json接受服务器端返回的类型
        timeout : 3000    // 限制请求的时间，当请求大于3秒后，跳出请求
    }
    function showRequest(formData, jqForm, options){
        return true;
    }
    function showResponse(responseText,statusText){
        cg_alert_add(responseText,'/admin_medicine','/admin_medicine/');
    }
function cg_alert_add(obj,list_url,add_url){
    if(obj.status == 1){
        swal({
            title: obj.msg,
            confirmButtonText:'继续添加',
            confirmButtonColor: "#DD6B55",
            showCancelButton: true,
            cancelButtonText:'返回列表',
            type: "success",
        },function(isConfirm){
            if (isConfirm) {
                location.href = add_url;
            }else{
                location.href = list_url;
            }
        });
    }else{
        swal(obj.msg,'','error');
    }

}
	var offalert="<div class='off-alert'></div>",
        html = "<div class='mark marks'></div>"
                  +"<form method='post' class='form-incline addform' action='/admin_medicine/add' class='addMedicine'>"
               +"<div class='off-alert off-alert-yyk'>"
                  +"<h3>新增药品<span id='close'>×</span></h3>"
                  +"<div class='inputs now-med'><label>药品名</label><input type='text' name='name' style='width:200px'><br><label>备注　</label><input type='text' style='width:500px'><label style='padding-top:25px'>添加图片</label><p class='bg-have'><img id='img_view' name='photo' src='' width='100' height='100'><input type='file' name='img' id='file_img'></p></div>"
                  +"<div class='savecancel'><button type='button' id='add-btn' >保存</button><button type='button' class='close1'>取消</button></div>"
          +"</div>"
                  +"</form>";
       
      $("#off-btn").click(function(){
      	  $("body").prepend($(html))
          $(".marks").css("height",$(document).height()+"px")       
          $(".marks").addClass('hidden')    
          $(".off-alert").addClass('hidden')
           $('.marks').show()
           $('.off-alert').show()
           $(".off-alert").removeClass('hidden')
           $(".marks").removeClass('hidden')  
            $('#add-btn').click(function(){
                $('.addform').ajaxSubmit(options);
            });
    $("#close").click(function(){
           $('.marks').hide()
           $('.off-alert').hide()
           $(".off-alert").addClass('hidden')
           $(".marks").addClass('hidden')
     })
    $(".close1").click(function(){
           $('.marks').hide()
           $('.off-alert').hide()
           $(".off-alert").addClass('hidden')
           $(".marks").addClass('hidden')
     })
var result = document.getElementById('img_view');
var input = document.getElementById('file_img');
if(typeof FileReader==='undefined'){
    result.innerHTML = '抱歉，你的浏览器不支持 FileReader';
    input.setAttribute('disabled','disabled');
}else{
    input.addEventListener('change',readFile,false);
}
function readFile(){
    var file = this.files[0];
    if(!/image\/\w+/.test(file.type)){
        alert('文件必须为图片');
        return false;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e){
        $('img[name="photo"]').attr('src',this.result);
    }
}
     })

})
$(function(){
//提交
    var options={
        beforeSubmit : showRequest,    // 提交前的回调函数
        success : showResponse,    // 提交后的回调函数
        dataType : "json",    // html（默认）、xml、script、json接受服务器端返回的类型
        timeout : 3000    // 限制请求的时间，当请求大于3秒后，跳出请求
    }
    function showRequest(formData, jqForm, options){
        return true;
    }
    function showResponse(responseText,statusText){
        cg_alert_edit(responseText,'/admin_medicine','/admin_medicine/');
    }
function cg_alert_edit(obj,list_url,edit_url){
    if(obj.status == 1){
        swal({
            title: obj.msg,
            confirmButtonText:'继续修改',
            confirmButtonColor: "#DD6B55",
            showCancelButton: true,
            cancelButtonText:'返回列表',
            type: "success",
        },function(isConfirm){
            if (isConfirm) {
                location.href = edit_url;
            }else{
                location.href = list_url;
            }
        });
    }else{
        swal(obj.msg,'','error');
    }

}
	var offalert="<div class='off-alert-edit'></div>",
        html = "<div class='mark-edit marks-edit'></div>"
                  +"<form method='post' class='form-incline editform' action='/admin_medicine/edit' class='editMedicine'>"
               +"<div class='off-alert-edit off-alert-yyk-edit'>"
                  +"<h3>编辑药品<span class='closebtn'>×</span></h3>"
                  +"<div class='inputs now-med'><label>药品名</label><input type='text' name='name' class='name' style='width:200px' id='med-input'><br><label>备注　</label><input type='text' name='remarks' class='remarks' style='width:500px'><label style='padding-top:25px'>添加图片</label><input type='hidden' name='imgnew'><input type='hidden' name='tId' class='tId'><p id='med-img' class='p'></p><div style='display:none'><input type='file' name='img' id='file_editimg'></div></div>"
                  +"<div class='savecancel'><button type='button' id='edit-btn'>保存</button><button type='button' id='cancleBtn'>取消</button></div>"
          +"</div>"
                  +"</form>";
       
        $("body").prepend($(html))
       $(".marks-edit").css("height",$(document).height()+"px")       
   $(".marks-edit").addClass('hidden')    
   $(".off-alert-edit").addClass('hidden')
    $('.edit').each(function(){
        $(this).click(function(){
            $('.marks-edit').show()
            $('.off-alert-edit').show()
            $('.off-alert-edit').removeClass('hidden')
            $('.marks-edit').removeClass('hidden')
            $('#med-img').html($(this).parent().parent().find('td').eq(1).html())
            var d_id = $(this).attr('data');
            $.get('/admin_medicine',{d_id:d_id},function(obj){
                if(obj.status == 1){
                    var data = obj.data;
                    $('input[class="name"]').val(data.name);
                    $('input[class="remarks"]').val(data.remarks);
                    $('input[class="tId"]').val(data.id);
                }
            });
            $('.p').click(function(){
                $('#file_editimg').click();
            });
var input = document.getElementById('file_editimg');
if(typeof FileReader==='undefined'){
    //result.innerHTML = '抱歉，你的浏览器不支持 FileReader';
    input.setAttribute('disabled','disabled');
}else{
    input.addEventListener('change',readFile,false);
}
function readFile(){
    var file = this.files[0];
    if(!/image\/\w+/.test(file.type)){
        alert('文件必须为图片');
        return false;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e){
        $('.p').after('<img src="'+this.result+'" width="100" height="100">');
    }
}
            $('#edit-btn').click(function(){
                $('.editform').ajaxSubmit(options);
            });
        });
    });
    $(".closebtn").click(function(){
           $('.marks-edit').hide()
           $('.off-alert-edit').hide()
           $(".off-alert-edit").addClass('hidden')
           $(".marks-edit").addClass('hidden')
     })
    $("#cancleBtn").click(function(){
           $('.marks-edit').hide()
           $('.off-alert-edit').hide()
           $(".off-alert-edit").addClass('hidden')
           $(".marks-edit").addClass('hidden')
     })

})
	    $('#search').click(function(){
	    $('.form-inline').submit();
		});
function cg_confirm_del(del_url,jump_url){
    swal({
            title: '您确定要删除吗？',
            text:'删除之后不能恢复',
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText:'确定',
            closeOnConfirm: false,
            cancelButtonText:'取消'
        },
        function(isConfirm){
            if(isConfirm){
                window.obj = '';
                $.ajax({
                    url: del_url,
                    dataType: "json",
                    async:false,
                    success: function (obj) {
                        window.obj = obj;
                    }
                });

                if(window.obj.status == 1){
                    swal({title:"删除成功!", type:"success"},function(){
                        location.href = jump_url;
                    });
                }else{
                    swal({title:window.obj.msg, type:"error"});
                }
            }
        });
}
    </script>
    
</body>
</html>
